package com.io.typescript.codegen;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Utils {

    public static void write(Set<String> set) {
//        StringBuilder sb = new StringBuilder();
//        for (String s : set) {
//            sb.append("\n").append(s);
//        }
//        write(sb.toString());
    }

    public static void write(List<String> list) {
//        StringBuilder sb = new StringBuilder();
//        for (String s : list) {
//            sb.append("\n").append(s);
//        }
//        write(sb.toString());
    }

    private static void write(String data) {
//        FileWriter fileWriter;
//        try {
//            String path = "D:/Develop/web/template/swagger.txt";
//            File file = new File(path);
//            if (!file.exists()) {
//                file.createNewFile();
//            }
//            fileWriter = new FileWriter(path);
//            fileWriter.write(data);
//            fileWriter.flush();
//            fileWriter.close();
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
    }

    /**
     * 在目录下生成一个日期文件
     *
     * @param dir 目录
     */
    public static void createNewFile(String dir) {
        try {
            if (null == dir || dir.equals("")) {
                return;
            }
            File fileDir = new File(dir);
            if (!fileDir.exists()) {
                fileDir.mkdirs();
            }
            String path = dir + "/" + getCurrentTimeStr() + ".txt";
            File file = new File(path);
            if (file.exists()) {
                if (file.delete()) {
                    file.createNewFile();
                }
            } else {
                file.createNewFile();
            }
//            FileWriter  fileWriter = new FileWriter(path);
//            fileWriter.write(data);
//            fileWriter.flush();
//            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getCurrentTimeStr() {
        String currentTime;
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.getDefault());
        currentTime = dateFormat.format(date);
        return currentTime;
    }

    public static boolean deleteAllFile(String path, boolean isIncludeRoot) {
        if (path == null) {
            return false;
        }
        System.out.println("---------------------------------path="+path);
        File file = new File(path);
        if (!file.exists()) {
            return false;
        }

        if (file.isFile()) {
            return file.delete();
        }

        File[] fileList = file.listFiles();
        boolean res = true;
        for (File f : fileList) {
            if (f.isFile()) {
                res = res && f.delete();
            } else if (f.isDirectory()) {
                res = res && deleteAllFile(f.getAbsolutePath(), true);
            }
        }

        if (isIncludeRoot) {
            res = res && file.delete();
        }

        return res;
    }
}
